package com.example.imagepicker;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lcw.library.imagepicker.utils.ImageLoader;
import com.lcw.library.imagepicker.utils.ScopedStorageUtil;

/**
 * Glide图片加载器实现
 * 支持Uri和文件路径，适配分区存储
 */
public class GlideImageLoader implements ImageLoader {

    private Context context;

    public GlideImageLoader(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    public void loadImage(ImageView imageView, String imagePath) {
        if (imagePath == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_failed);

        Glide.with(context)
                .load(imagePath)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadImage(ImageView imageView, Uri imageUri) {
        if (imageUri == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_failed);

        Glide.with(context)
                .load(imageUri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadPreImage(ImageView imageView, String imagePath) {
        if (imagePath == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_failed);

        Glide.with(context)
                .load(imagePath)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadPreImage(ImageView imageView, Uri imageUri) {
        if (imageUri == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_failed);

        Glide.with(context)
                .load(imageUri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadVideoThumbnail(ImageView imageView, String videoPath) {
        if (videoPath == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_failed);

        Glide.with(context)
                .load(videoPath)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void loadVideoThumbnail(ImageView imageView, Uri videoUri) {
        if (videoUri == null) {
            return;
        }

        RequestOptions options = new RequestOptions()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_failed);

        Glide.with(context)
                .load(videoUri)
                .apply(options)
                .into(imageView);
    }

    @Override
    public void clearMemoryCache() {
        Glide.get(context).clearMemory();
    }

    /**
     * 智能加载图片，优先使用Uri（适配分区存储）
     *
     * @param imageView
     * @param imagePath
     * @param imageUri
     */
    public void loadImageSmart(ImageView imageView, String imagePath, Uri imageUri) {
        // 在分区存储环境下优先使用Uri
        if (ScopedStorageUtil.shouldUseUri(context) && imageUri != null) {
            loadImage(imageView, imageUri);
        } else if (imagePath != null) {
            loadImage(imageView, imagePath);
        } else if (imageUri != null) {
            loadImage(imageView, imageUri);
        }
    }

    /**
     * 智能加载预览图片，优先使用Uri（适配分区存储）
     *
     * @param imageView
     * @param imagePath
     * @param imageUri
     */
    public void loadPreImageSmart(ImageView imageView, String imagePath, Uri imageUri) {
        // 在分区存储环境下优先使用Uri
        if (ScopedStorageUtil.shouldUseUri(context) && imageUri != null) {
            loadPreImage(imageView, imageUri);
        } else if (imagePath != null) {
            loadPreImage(imageView, imagePath);
        } else if (imageUri != null) {
            loadPreImage(imageView, imageUri);
        }
    }

    /**
     * 智能加载视频缩略图，优先使用Uri（适配分区存储）
     *
     * @param imageView
     * @param videoPath
     * @param videoUri
     */
    public void loadVideoThumbnailSmart(ImageView imageView, String videoPath, Uri videoUri) {
        // 在分区存储环境下优先使用Uri
        if (ScopedStorageUtil.shouldUseUri(context) && videoUri != null) {
            loadVideoThumbnail(imageView, videoUri);
        } else if (videoPath != null) {
            loadVideoThumbnail(imageView, videoPath);
        } else if (videoUri != null) {
            loadVideoThumbnail(imageView, videoUri);
        }
    }
} 